package baibeiwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.utils.ActionBarHelper;

import org.greenrobot.eventbus.EventBus;
import org.web3j.crypto.CipherException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.BaibeiWallet;
import baibeiwallet.WalletManager;
import baibeiwallet.event.CreateWalletEvent;
import baibeiwallet.event.SelWalletEvent;
import baibeiwallet.utils.BaiBeiWalletUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateWalletActivity1 extends BaseActivity {


    @BindView(R.id.leftItem)
    ImageView leftItem;
    @BindView(R.id.et_wallet_name)
    EditText etWalletName;
    @BindView(R.id.et_wallet_pwd)
    EditText etWalletPwd;
    @BindView(R.id.et_wallet_pwd_again)
    EditText etWalletPwdAgain;
    @BindView(R.id.btn_create_wallet1)
    TextView btnCreateWallet1;
    @BindView(R.id.btn_input_wallet)
    TextView btnInputWallet;
    private List<BaibeiWallet> mListWallets = new ArrayList<>();
    BaibeiWallet mWallet;

    @Override
    protected MyPreenter initPreenter() {
        return null;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_create_wallet3;
    }

    @Override
    protected void initVariables() {
        new ActionBarHelper(this).setTitle(getString(R.string.menu_hint_item3));
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void loadData() {

    }


    @OnClick(R.id.btn_create_wallet1)
    public void onClickCreate_wallet() {
        String walletName = etWalletName.getText().toString();
        if (TextUtils.isEmpty(walletName)) {
            showToast(getString(R.string.create_wallet_name_input_tips));
//            Toast.makeText(this, "请输入钱包名", Toast.LENGTH_SHORT).show();
            return;
        }
        String pwd1 = etWalletPwd.getText().toString();
        if (TextUtils.isEmpty(pwd1)) {
            showToast(getString(R.string.wallet_hint_pleaseinputpsw));
//            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        String pwd2 = etWalletPwdAgain.getText().toString();
        if (TextUtils.isEmpty(pwd2)) {
            showToast(getString(R.string.profile_hint_warn10));
//            Toast.makeText(this, "请输入重复密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pwd1.equalsIgnoreCase(pwd2)) {
            showToast(getString(R.string.profile_hint_warn13));
//            Toast.makeText(this, "两次输入的密码不同", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            mWallet = BaiBeiWalletUtils.generateBip44Wallet(pwd2);
            mWallet.setWalletName(walletName);
            mWallet.setPassword(pwd2);
            WalletManager.getInstance().addWallet(mWallet);
            String address = mWallet.getWalletFile().getAddress();
            Log.i("TAG", "onClickCreateWallet: 钱包地址 = " + address);
            if (!TextUtils.isEmpty(address)) {
                EventBus.getDefault().post(new CreateWalletEvent());
                EventBus.getDefault().post(new SelWalletEvent(0));
            }
            if (TextUtils.isEmpty(address)) {
                showToast(getString(R.string.wallet_hint_warn5));
//                Toast.makeText(this, "钱包地址为空", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent i = new Intent(CreateWalletActivity1.this, WalletDetailActivity1.class);
            i.putExtra("index", 0);
            startActivity(i);
            finish();

        } catch (CipherException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn_input_wallet)
    public void onClickimport_wallet() {
        startActivity(InprotWalletActivity1.class);
        finish();
    }

    @Override
    public void onBackPressed() {
//        if (!TextUtils.isEmpty(mTvAddress.getText().toString())) {
//            EventBus.getDefault().post(new CreateWalletEvent());
//        }
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {

    }
}
